package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Resource;
import com.itheima.domain.ResourceCategory;
import com.itheima.mapper.ResourceCategoryMapper;
import com.itheima.mapper.ResourceMapper;
import com.itheima.service.IResourceCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资源分类表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMapper, ResourceCategory> implements IResourceCategoryService {
    @Autowired
    ResourceCategoryMapper resourceCategoryMapper;
    @Autowired
    ResourceMapper resourceMapper;

    @Override
    public List<ResourceCategory> findAll() {
        QueryWrapper<ResourceCategory> qw=new QueryWrapper<>();
        return resourceCategoryMapper.selectList(qw);
    }

    @Override
    public void saveCategory(ResourceCategory resourceCategory) {
        resourceCategoryMapper.insert(resourceCategory);
    }

    @Override
    public Boolean deleteById(Long id) {
        //判断资源分类下是否含有资源
        LambdaQueryWrapper<Resource> qw=new LambdaQueryWrapper<>();
        qw.eq(Resource::getCategoryId,id);
        List<Resource> resources = resourceMapper.selectList(qw);
        if(resources.size()==0){//若不含资源直接删除
            resourceCategoryMapper.deleteById(id);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void updateCategory(ResourceCategory resourceCategory) {
        resourceCategoryMapper.updateById(resourceCategory);
    }
}
